package com.ruoyi.my.RabbitMQ;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQReceive {

    //使用的是Jackson库中的Jackson2JsonMessageConverter类，代替使用jdk自带的序列
    @Bean
    public MessageConverter messageConverter() {
        //创建一个Jackson2JsonMessageConverter 类型的 "消息转换器组件"
        return new Jackson2JsonMessageConverter();
    }

    /******开始*****主题模式的消费者******/
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "华东地区交换机",type = "topic"),//topic是主题模式的交换机
            key = {"杭州"},
            value = @Queue("e")
    ))
    public void ztlfzf(String message){
        System.out.println("杭州政府收到消息，准备撸起袖子干："+message);
    }
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "华东地区交换机",type = "topic"),//topic是主题模式的交换机
            key = {"南京.*"}, //*代表南京下一级所有的区政府
            value = @Queue("e")
    ))
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "华东地区交换机",type = "topic"),//topic是主题模式的交换机
            key = {"上海.#"},//#代表上海所有子级政府
            value = @Queue("e")
    ))
    public void ztbj(String message){
        System.out.println("上海政府下所有的区收到消息，准备撸起袖子干："+message);
    }
    /******结束*****主题模式的消费者******/


    /******开始*****路由模式的消费者******/
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "华北地区交换机",type ="direct" ),//direct是路由模式的交换机
            key = {"石家庄"},
            value = @Queue("e")
    ))
    public void sjzzf(String message){
        System.out.println("石家庄政府收到消息，准备撸起袖子干："+message);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "华北地区交换机",type = "direct"),//direct是路由模式的交换机
            key = {"廊坊"},
            value = @Queue("e")
    ))
    public void lfzf(String message){
        System.out.println("廊坊政府收到消息，准备撸起袖子干："+message);
    }
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "华北地区交换机",type = "direct"),//direct是路由模式的交换机
            key = {"北京.海淀"},
            value = @Queue("e")
    ))
    public void bj(String message){
        System.out.println("北京.海淀政府收到消息，准备撸起袖子干："+message);
    }
    /******结束*****路由模式的消费者******/


    /******开始*****订阅模式的消费者******/
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue1", durable = "true"),
            exchange = @Exchange(value = "xyz抖音队列", type = "fanout"),
            key = ""
    ))
    public void receive(String message) {
        System.out.println("因为我订阅了这个'xyz抖音号'，所以收到了一条视频推送': " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue2", durable = "true"),
            exchange = @Exchange(value = "xyz抖音队列", type = "fanout"),
            key = ""
    ))
    public void receive2(String message) {
        System.out.println("因为我订阅了这个'xyz抖音号'，所以收到了一条视频推送': " + message);
    }
    /******结束*****订阅模式的消费者******/



    /******开始*****简单模式的消费者******/
    @RabbitListener(queuesToDeclare = @Queue("邮件队列"))
    public void helloWorld(String message){
        System.out.println("推送到小明邮件箱，请查收: " + message);
    }
    /******结束*****简单模式的消费者******/



}